package ubi.quando.UI;

import java.util.List;

import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.overlay.Overlay;
import org.osmdroid.views.overlay.ScaleBarOverlay;
import org.osmdroid.views.overlay.SimpleLocationOverlay;

import ubi.quando.App.Contact;
import ubi.quando.App.EventBase;
import ubi.quando.App.EventMeeting;
import ubi.quando.App.EventPickUp;
import ubi.quando.App.EventType;
import ubi.quando.App.PickUpParticipant;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

public class EventRunningActivity extends Activity {

	UbiApplication mApp;
	private EventBase mEvent;

	private TextView mTxtEventName;
	private Button mBtnMinimize;
	private MapViewUbi mMapView;
	private MapController mMapController;

	private SimpleLocationOverlay mMyPosition;
	private SimpleLocationOverlay mDestination;
	private ScaleBarOverlay mScaleBarOverlay;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.running_event);
		mApp = (UbiApplication) getApplication();
		mApp.setCurrentActivity(this);

		Bundle b = getIntent().getExtras();
		if (b != null) {
			mEvent = (EventBase) b.getSerializable(EventBase.SerializeName);
		}

		mMapView = (MapViewUbi) findViewById(R.id.mapview);
		mTxtEventName = (TextView) findViewById(R.id.txt_eventName);
		mBtnMinimize = (Button) findViewById(R.id.btn_minimize);

		mTxtEventName.setText(mEvent.GetName());

		mMapView.setTileSource(TileSourceFactory.MAPNIK);
		mMapView.setBuiltInZoomControls(true);
		mMapView.setMultiTouchControls(true);

		mDestination = new SimpleLocationOverlay(this);
		mDestination.setLocation(mEvent.GetDestination());
		mMapView.getOverlays().add(mDestination);
		mMyPosition = new SimpleLocationOverlay(this);

		mMapController = mMapView.getController();
		mMapController.setZoom(12);
		mMapController.setCenter(new GeoPoint(UbiApplication.DEFAULT_LAT,
				UbiApplication.DEFAULT_LONG));

		mScaleBarOverlay = new ScaleBarOverlay(this);
		mMapView.getOverlays().add(mScaleBarOverlay);

		initFont();

		invalidateParticipants();

		mMapView.invalidate();
	}

	public void invalidateParticipants() {
		 new InvalidateParticipants().execute();
	}

	private void initFont() {
		Typeface font = Typeface.createFromAsset(getAssets(), "AGENCYR.TTF");

		mBtnMinimize.setTypeface(font);
		mTxtEventName.setTypeface(font);
	}

	public void UpdateParticipantsLocation(String clientId, String location) {
		String[] latlng = location.split(",");
		GeoPoint loc = new GeoPoint(Integer.parseInt(latlng[0]),
				Integer.parseInt(latlng[1]));

		if (mEvent.getType() == EventType.MEETING) {
			List<Contact> participants = ((EventMeeting) mEvent)
					.getParticipants();
			for (Contact contact : participants) {
				if (contact.getId().equals(clientId)) {
					contact.setLocation(loc);
					break;
				}
			}
		} else {
			List<PickUpParticipant> participants = ((EventPickUp) mEvent)
					.getParticipants();
			for (PickUpParticipant contact : participants) {
				if (contact.getContact()/*.getId()*/.equals(clientId)) {
					mApp.getContactById(contact.getContact()).setLocation(loc); //TODO check it
					break;
				}
			}
		}

		invalidateParticipants();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mApp.setCurrentActivity(this);
	}
	
	private class InvalidateParticipants extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... arg0) {
			List<Overlay> overlays = mMapView.getOverlays();
			SimpleLocationOverlay overlay;

			overlays.clear();

			if (mEvent.getType() == EventType.MEETING) {
				List<Contact> participants = ((EventMeeting) mEvent)
						.getParticipants();
				for (Contact contact : participants) {
					overlay = new SimpleLocationOverlay(UbiApplication.CONTEXT);
					overlay.setLocation(contact.getLocation());
					overlays.add(overlay);
				}
			} else {
				List<PickUpParticipant> participants = ((EventPickUp) mEvent)
						.getParticipants();
				for (PickUpParticipant contact : participants) {
					overlay = new SimpleLocationOverlay(UbiApplication.CONTEXT);
					overlay.setLocation(contact.getLocation());
					overlays.add(overlay);
					
					overlay = new SimpleLocationOverlay(UbiApplication.CONTEXT);
					overlay.setLocation(mApp.getContactById(contact.getContact()).getLocation()); //TODO check it
					overlays.add(overlay);
				}
			}
			
			mDestination.setLocation(mEvent.GetDestination());
			mMapView.getOverlays().add(mDestination);
			
			mMapView.getOverlays().add(mMyPosition);
			
			return null;		
		}	
		
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			mMapView.invalidate();
		}
	 }
}
